﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using System.Data;
using CNVP.JShop.Web.Controls;
using CNVP.Framework;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class ProductTypeList : AdminPage
    {
        BLL.CNVP_ProductType bll = new BLL.CNVP_ProductType();
        public int rplistCount = 0;
        int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 71)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!IsPostBack)
            {
                StartLoad(PageIndex);
                #region 验证
                btDelete.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定删除该记录吗?');}else{alert('请选择记录！');return false;};");
                #endregion 
            }
           
        }


        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dt = null;
            int pageNum = 20;
            dt = bll.GetPage(this.GetType().Name, PageIndex, pageNum, out i, out j, null);
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    int code = int.Parse(cbCheck.ToolTip);
                    if (new BLL.CNVP_ProTypeRefBranks().GetSingle_CNVP_ProTypeRefBranksByProductTypeID(code).BranksIDS != "")
                    {
                        BasePage.MessageBoxGoHistory("该类型存在关联品牌,不允许删除.！");
                        return;
                    }
                    else
                    {
                        new BLL.CNVP_ProTypeRefBranks().Delete_CNVP_ProTypeRefBranks(code);
                    }
                    if(new BLL.CNVP_ProTypeRefStandard().GetAll_CNVP_ProTypeRefStandard(code).Count>0)
                    {
                        BasePage.MessageBoxGoHistory("该类型存在规格,不允许删除.！");
                        return;
                    }
                    if (new BLL.CNVP_ProTypeRefExtendProperty().GetAll_CNVP_ProTypeRefExtendProperty(code).Count > 0)
                    {
                        BasePage.MessageBoxGoHistory("该类型存在扩展属性,不允许删除.！");
                        return;
                    }
                    if (new BLL.CNVP_ProTypeRefDescripttion().GetAll_CNVP_ProTypeRefDescripttion(code).Count > 0)
                    {
                        BasePage.MessageBoxGoHistory("该类型存在详细参数表,不允许删除.！");
                        return;
                    }
                    if (new BLL.CNVP_ProTypeRefShopMustInfo().GetAll_CNVP_ProTypeRefShopMustInfo(code).Count > 0)
                    {
                        BasePage.MessageBoxGoHistory("该类型存在购物必填,不允许删除.！");
                        return;
                    }
                    var Column_List = new BLL.CNVP_Column().GetAll_CNVP_Column("ProductTypeID=" + code + "");
                    if (Column_List.Count > 0)
                    {
                        BasePage.MessageBoxGoHistory("该类型存在商品分类中,不允许删除.！");
                        return;
                    }
                    var Products_List = new BLL.CNVP_Products().GetAll_CNVP_Products();
                    foreach (Model.CNVP_Products p in Products_List)
                    {
                        if (p.ProductTypeID == code)
                        {
                            BasePage.MessageBoxGoHistory("该类型存在商品列表中,不允许删除.！");
                            return;
                        }
                    }
                    bll.Delete_CNVP_ProductType(code);
                }
            }
            StartLoad(PageIndex);
        }

        protected void btAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductTypeEdit.aspx");
        }
    }
}